
package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;

/**
 * 汕头网友社区 网站解析
 * @author Chase
 *
 */
public class YunvbbsAnalyse implements AnalyseNews {

	private static final String[] regex = {
		"http://www.zhuhaiyunv.com/bbs/read-htm-tid-[0-9]*.html"
	};


	
	public boolean isDetailPage(String url) {
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta meta = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String url = urlMeta.getUrl();
		/*
		 * 网站源码
		 */
		String htmltxt = urlMeta.getHtml();

		if(!isDetailPage(url)){
		}


		String title = null;
		String content = null;
		String author = null;
		Long date = null;
		Document doc = Jsoup.parse(htmltxt);
		Integer ClickNum = null;
		Integer CommentNum = null;

		/*
		 * 标题
		 */
		title = doc.select("h1.read_h1").text();

		/*
		 * 时间
		 */
		date = DateUtils.matchDate(doc.select(".s6").text());

		if(StringUtils.isBlank(title) || date == null){
		}

		if(doc.select("ul.cc em").size() > 1){
			ClickNum = NumberUtils.matchNumber(doc.select("ul.cc em").get(0).text());
			CommentNum = NumberUtils.matchNumber(doc.select("ul.cc em").get(1).text());
		}
		/*
		 * 内容
		 */
		String contentStr = null;
		content = HtmlCleaner.getContentHtml(url,  doc.select(".tpc_content"));
//		if(StringUtils.isBlank(content))
//			content = doc.select("td.hidden").html();

		/*
		 * 作者
		 */
		if( doc.select(".readName a").size() > 0)
			author = doc.select(".readName a").get(0).text();
//		if(StringUtils.isBlank(author))
//			author = doc.select(".rowd").get(0).text();
		/*
		 * 链接
		 */
		meta.setUrl(url);

		/*
		 * 来源
		 */
		meta.setTitle(title);
		meta.setContent(content);
		meta.setAuthor(author);
		meta.setDate(date);
		meta.setClickNum(ClickNum);
		meta.setCommentNum(CommentNum);


		return meta;
	}

	

	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}


	
}
